using System;
using System.Linq;
using UnityEngine;
using UnityEditor;
using UnityEditorInternal;

namespace CopyPasteComponents.Editor
{
    [CustomEditor(typeof(ComponentsIgnoreList))]
    public class ComponentSearchLauncher : UnityEditor.Editor
    {
        private ReorderableList list;
        private SerializedProperty IgnoreListProperty;

        Rect buttonRect;
        ComponentSearchPopup searchPopup;

        private ComponentsIgnoreList componentsIgnoreList { get { return target as ComponentsIgnoreList; } }

        private void OnEnable()
        {
            IgnoreListProperty = serializedObject.FindProperty("IgnoreList");

            // 初始化 ReorderableList
            list = new ReorderableList(serializedObject, IgnoreListProperty, true, true, false, true)
            {
                // 绘制列表项
                drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) =>
                {
                    float indentLevel = 0; // 可以根据需要调整缩进级别
                    Rect labelRect = new Rect(rect.x + indentLevel, rect.y + (rect.height - EditorGUIUtility.singleLineHeight) / 2, 40, EditorGUIUtility.singleLineHeight);
                    Rect textFieldRect = new Rect(labelRect.xMax, rect.y + (rect.height - EditorGUIUtility.singleLineHeight) / 2, rect.width - labelRect.width, EditorGUIUtility.singleLineHeight);

                    // 显示索引
                    EditorGUI.LabelField(labelRect, index.ToString());

                    // 绘制字符串字段
                    var element = list.serializedProperty.GetArrayElementAtIndex(index);
                    //EditorGUI.PropertyField(textFieldRect, element, GUIContent.none);
                    string text = element.stringValue.Split(',')[0];
                    EditorGUI.TextField(textFieldRect, text);
                },

                drawHeaderCallback = rect =>
                {
                    EditorGUI.LabelField(rect, "IgnoreList", EditorStyles.boldLabel);
                },

                // 当移除元素时触发
                onRemoveCallback = (ReorderableList l) =>
                {
                    if (EditorUtility.DisplayDialog("确认删除", "确定要删除此元素吗？", "删除", "取消"))
                    {
                        var element = list.serializedProperty.GetArrayElementAtIndex(l.index);
                        OnRemoveElement(element.stringValue);
                        ReorderableList.defaultBehaviours.DoRemoveButton(l);
                    }
                }
            };

            searchPopup = new ComponentSearchPopup(SelectCallBack);
        }

        public override void OnInspectorGUI()
        {
            serializedObject.Update();
            EditorGUILayout.BeginVertical();

            // 使用 ReorderableList 来绘制列表
            list.DoLayoutList();

            if (GUILayout.Button("Add Ignore Component", GUILayout.Height(25)))
            {
                PopupWindow.Show(buttonRect, searchPopup);
            }

            if (Event.current.type == EventType.Repaint)
            {
                buttonRect = GUILayoutUtility.GetLastRect();
                buttonRect.x += (buttonRect.width - searchPopup.width) / 2;
                buttonRect.y += 10;
            }


            GUILayout.EndVertical();

            serializedObject.ApplyModifiedProperties();
        }

        private void SelectCallBack(Type type)
        {
            componentsIgnoreList.AddType(type.AssemblyQualifiedName);
        }

        private void OnRemoveElement(string type)
        {
            componentsIgnoreList.RemoveType(type);
        }
    }
}

